package org.jit.banking;

import static java.lang.System.*;

/**
 * @author 水杉metasequoia
 */
public class CheckingAccount extends Account{
    /** 透支额度 */
    private double overdraftProtection;
    /** 为了在还款的时候能先还上已透支的额度，必须要知道"原始透支额度"，所以即使类图里没有，我也必须设置这个变量。*/
    private double privateOverdraftProtection;

    public CheckingAccount(double balance,double protect){
        super.balance = balance;
        privateOverdraftProtection = protect;
        overdraftProtection = protect;
    }

    public double getPrivateOverdraftProtection() {
        return privateOverdraftProtection;
    }

    public void setPrivateOverdraftProtection(double privateOverdraftProtection) {
        this.privateOverdraftProtection = privateOverdraftProtection;
    }

    public double getOverdraftProtection() {
        return overdraftProtection;
    }

    @Override
    public boolean withdraw(double awt){
        if(awt<=balance){
            balance -= awt;
            out.println("钱已取出。");
            return true;
        }
        else if(awt<=(balance+overdraftProtection)) {
            overdraftProtection -= awt-balance;
            balance = 0;
            out.println("已透支，透支额度剩余："+overdraftProtection);
            return true;
        }
        else {
            return false;
        }
    }

    @Override
    public boolean deposit(double awt){
        // 没透支
        if(overdraftProtection==privateOverdraftProtection)
        {
            balance += awt;
            out.println("已存入账户余额");
        }
        //透支了，存入金额还回已透支额度，没有剩款进余额
        else if(awt<=privateOverdraftProtection-overdraftProtection)
        {
            overdraftProtection += awt;
            out.println("已偿还部分透支额度");
        }
        //透支了，存入金额还完已透支额度，还有剩款进余额
        else {
            overdraftProtection = privateOverdraftProtection;
            balance += awt-(privateOverdraftProtection-overdraftProtection);
            out.println("透支额度已还清，剩余钱款已存入账户余额");
        }
        return true;
    }

}
